package lol.clann.minecraft.springboot.plugin.bukkit.register;

import lombok.extern.slf4j.Slf4j;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

/**
 * 自动注册监听器
 * @author pyz
 * @date 2019/4/8 9:35 PM
 */
@Slf4j
@Component
@ConditionalOnClass(Bukkit.class)
public class ListenerReigster implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Listener) {
            Plugin plugin = Bukkit.getPluginManager().getPlugin("SpringBootPlugin");
            Listener listener = (Listener) bean;
            Bukkit.getPluginManager().registerEvents(listener, plugin);
            LOGGER.info("register listener:" + listener.getClass().getName());
        }
        return bean;
    }

}
